package com.cos.controller;

import com.cos.commons.R;
import com.cos.constant.CookieID;
import com.cos.constant.LoginSuccess;
import com.cos.constant.Sms;
import com.cos.fegin.api.CarService;
import com.cos.fegin.api.ItemService;
import com.cos.constant.Redis;
import com.cos.utils.PasswordUils;
import com.cos.vo.Customer;
import com.cos.vo.SmsVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private ItemService itemService;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private CarService carService;
    @RequestMapping("/checkUsername")
    @ResponseBody
    public Map<String,Object> checkUsername( String param){
        Customer customer = itemService.checkUsername(param);
        Map<String,Object> map=new HashMap<>();
        if (customer==null) {
            map.put("status","y");
            map.put("info","可注册");
        }else {
            map.put("status","n");
            map.put("info","用户名已存在,不可注册");
        }
        return map;
    }

        @RequestMapping("/sendSms/{phone}")
        @ResponseBody
        public void sendSms(@PathVariable String phone){
            System.out.println(phone);
            //生成验证码
            String random = RandomStringUtils.random(6, false, true);
            //将生成的验证码保存到redis中
            redisTemplate.opsForValue().set(Redis.REDIS_YANZHENGMA +phone,random,60, TimeUnit.SECONDS);
            SmsVo smsVo=new SmsVo();
            smsVo.setCode(random);
            smsVo.setPhone(phone);
            rabbitTemplate.convertAndSend(Sms.SMS_EXCHANGE,"goods.sms.code",smsVo);
    }

    @RequestMapping("/addUser")
    @ResponseBody
    public R addUser(Customer customer){
        System.out.println(customer);
        //判断验证码是否过期和输入的验证码是否和redis中的一样
        String s = redisTemplate.opsForValue().get(Redis.REDIS_YANZHENGMA + customer.getPhone());
        if (StringUtils.isEmpty(s)||!customer.getCode().equals(s)) {
            return R.error("验证码已过期或验证码错误");
        }
        //加密密码
        customer.setPassword(PasswordUils.hashpw(customer.getPassword()));
        boolean b = itemService.addUser(customer);
        if (b) {
            return R.success();
        }else {
            return R.error("注册失败!");
        }
    }

    @RequestMapping("/login")
    @ResponseBody
    public R login(Customer customer, HttpServletRequest request, @CookieValue(name = CookieID.COOKIE_NAME,defaultValue = "") String anneid){
        Customer customer1 = itemService.checkUsername(customer.getUsername());
        if (customer1==null) {
            return R.error("用户名或者密码错误,情重新输入!");

        }
        //判断输入的密码是否与查询的密码一致
        boolean checkPassword = PasswordUils.checkPassword(customer.getPassword(),customer1.getPassword());
        if (!checkPassword) {
            return R.error("用户名或者密码错误,情重新输入!");
        }
        //将用户信息保存到session中去
        request.getSession().setAttribute(LoginSuccess.LOGIN_SUCCESS,customer1);
        if (!StringUtils.isEmpty(anneid)){
            //合并购物车
            carService.mergeCar(customer1.getId(),anneid);
        }
        return R.success();
    }

    @RequestMapping("/loginOut")
    public String loginOut(HttpServletRequest request){
        request.getSession().invalidate();
        return "redirect:http://localhost/auth/login.html";
    }

    public static void main(String[] args) {

    }
}
